<template>
	<view>
		<!-- 顶部样式 -->
		<view class="head">
			<view :class="{'active':tabIndex==index}" class="title" v-for="(item,index) in titleList" :key="index" @tap="change(index)">
				<text>{{ item }}</text>
			</view>
		</view>

		<!-- 占位符 -->
		<view class="place"></view>

		<view v-if="tabIndex==0">
			<view class="items" v-for="(item,index) in list" :key="index" @tap="toDetail(item)">
				<view class="mainImg">
					<image :src="url+item.image" mode=""></image>
				</view>
				<view class="title">{{ item.title }}</view>
				<view class="items-content">
					<view class="left">
						<view class="time">活动时间:{{ doreset(item.begintime) }}</view>
						<view class="time">活动地点:{{ item.remark }}</view>
					</view>
					<view class="right">
						<view class="price">¥<text>{{ item.cost.toFixed(2) }}</text></view>
						<view class="timer">已有{{ item.hasMan+item.hasWoman }}人参加</view>
					</view>
				</view>
				<!-- 状态值 -->
				<view class="status">
					<i class="iconfont iconjinhangzhong statusIcon" v-if="item.status==1" style="color: #0091FF;"></i>
					<i class="iconfont iconyijieshu statusIcon" v-else style="color: #CCCCCC;"></i>
				</view>
			</view>

		</view>

		<view v-if="tabIndex==1">
			<view class="items" v-for="(item,index) in list" :key="index" @tap="toDetail(item)">
				<view class="mainImg">
					<image :src="url+item.image" mode=""></image>
				</view>
				<view class="title">{{ item.title }}</view>
				<view class="items-content">
					<view class="left">
						<view class="time">活动时间:{{ doreset(item.begintime) }}</view>
						<view class="time">活动地点:{{ item.remark }}</view>
					</view>
					<view class="right">
						<view class="price">¥<text>{{ item.cost.toFixed(2) }}</text></view>
						<view class="timer">已有{{ item.hasMan+item.hasWoman }}人参加</view>
					</view>
				</view>
				<!-- 状态值 -->
				<view class="status">
					<i class="iconfont iconjinhangzhong statusIcon" style="color: #0091FF;"></i>
				</view>
			</view>

		</view>

		<view v-if="tabIndex==2">
			<view class="items" v-for="(item,index) in list" :key="index" @tap="toDetail(item)">
				<view class="mainImg">
					<image :src="url+item.image" mode=""></image>
				</view>
				<view class="title">{{ item.title }}</view>
				<view class="items-content">
					<view class="left">
						<view class="time">活动时间:{{ doreset(item.begintime) }}</view>
						<view class="time">活动地点:{{ item.remark }}</view>
					</view>
					<view class="right">
						<view class="price">¥<text>{{ item.cost.toFixed(2) }}</text></view>
						<view class="timer">已有{{ item.hasMan+item.hasWoman }}人参加</view>
					</view>
				</view>
				<!-- 状态值 -->
				<view class="status">
					<i class="iconfont iconyijieshu statusIcon" style="color: #CCCCCC;"></i>
				</view>
			</view>


		</view>

		<!-- 返回顶部 -->
		<view class="back" v-if="goBack" @tap="toTop()">
			<image src="/static/images/index/back.png" mode=""></image>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token', 'userId'])
		},
		data() {
			return {
				page: 1,
				pages: '',
				list: [],
				time: '',
				url: '',
				tabIndex: 0,
				titleList: ['全部', '进行中', '已结束'],
				goBack: false
			};
		},
		//分享给个人
		onShareAppMessage() {
			return {
				title: '青年交友',
				imageUrl:'https://friend.tanshishang.cn/assets/img/shareImg.png',
				path:'/pages/index/index'
			}
		},
		//分享到朋友圈
		onShareTimeline(){
			return {
				title: '青年交友',
				path:'/pages/index/index'
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			uni.showLoading({
				title: '正在刷新...'
			})

			setTimeout(() => {
				uni.hideLoading();
				uni.stopPullDownRefresh();
				this.doreset();
			}, 1000)
		},
		//监听页面滚动
		onPageScroll(e) {
			// console.log(e);
			if (e.scrollTop >= 500) {
				//显示返回顶部按钮
				this.goBack = true;
			} else {
				this.goBack = false;
			}
		},
		//上拉加载
		onReachBottom() {
			console.log('触发上拉加载');
			if (this.page < this.pages) {
				this.page++;
				if (this.tabIndex == 0) {
					this.getData();
				} else if (this.tabIndex == 1) {
					this.getData1(this.tabIndex);
				} else if (this.tabIndex == 2) {
					this.getData1(this.tabIndex);
				}

			} else {
				uni.showToast({
					title: '到底了!',
					icon: 'none'
				})
			}

		},
		onReady() {

		},
		onLoad() {
			console.log('token:' + this.token);
			this.url = this.$config.API;

			//获取当前时间戳
			this.time = new Date().getTime();

			this.doChoose();

		},
		onShow() {
			// this.getMsg();
		},
		methods: {
			//获取消息数
			async getMsg() {
				// let token = uni.getStorageSync('token');
				// let userId = uni.getStorageSync('userId');
				// console.log(token);
				if (this.token) {
					try {
						let params = {
							token: this.token,
							userId: this.userId
						}
						let res = await this.$request.request.get('/api/user/noRead', params);
						console.log(res.data);
						this.$common.setCartBadge(res.data.count);
					} catch (err) {
						this.$request.error(err, this)
					}
				}

			},
			doChoose() {
				if (this.tabIndex == 0) {
					this.page = 1;
					this.pages = '';
					this.list = [];
					this.getData();

				} else if (this.tabIndex == 1) {
					this.page = 1;
					this.pages = '';
					this.list = [];
					this.getData1(this.tabIndex);

				} else if (this.tabIndex == 2) {
					this.page = 1;
					this.pages = '';
					this.list = [];
					this.getData1(this.tabIndex);
				}
			},
			//获取数据
			async getData() {
				try {
					let params = {
						token: this.token,
						type: 0,
						page: this.page
					};
					let res = await this.$request.request.get('/api/active/index', params)
					console.log(res);
					if (res.code == 200) {
						this.pages = res.data.pages;
						this.list = this.list.concat(res.data.list);
						for (let i in this.list) {
							if (this.time <= this.list[i].endtime * 1000) {
								//进行中
								this.list[i].status = 1;
							} else if (this.time > this.list[i].endtime * 1000) {
								//已结束
								this.list[i].status = 0;
							}
						}
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			//获取数据
			async getData1(e) {
				try {
					let params = {
						token: this.token,
						type: e,
						page: this.page
					};
					let res = await this.$request.request.get('/api/active/index', params)
					console.log(res);
					if (res.code == 200) {
						this.pages = res.data.pages;
						this.list = this.list.concat(res.data.list);
						for (let i in this.list) {
							if (this.time <= this.list[i].endtime * 1000) {
								//进行中
								this.list[i].status = 1;
							} else if (this.time > this.list[i].endtime * 1000) {
								//已结束
								this.list[i].status = 0;
							}
						}
					}
				} catch (err) {
					this.$request.error(err, this)
				}
			},
			//前往详情页
			toDetail(e) {
				console.log(e);
				// if (!this.token) {
				// 	uni.showModal({
				// 		title: '您还未登录',
				// 		content: '点击登录,登录后可查看更多!',
				// 		showCancel: true,
				// 		confirmText: '登录',
				// 		success: (res) => {
				// 			// console.log(res);
				// 			if (res.confirm) {
				// 				uni.navigateTo({
				// 					url: "/pages/index/wxLogin"
				// 				})
				// 			} else if (res.cancel) {
				// 				console.log('用户点击取消');
				// 			}

				// 		}
				// 	});
				// 	return false;
				// }
				uni.navigateTo({
					url: "activitiesDetail?id=" + e.id + '&status=' + e.status
				})
			},
			//顶部切换
			change(e) {
				this.tabIndex = e;
				this.doChoose();
			},
			//点击返回顶部
			toTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 600,
				});
			},
			//转换时间戳
			doreset(e) {
				//转换时间戳
				if ((e + '').length != 13) {
					var date = new Date(e * 1000); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
				} else {
					var date = new Date(e); //如果date为13位不需要乘1000
					var Y = date.getFullYear() + '-';
					var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
					var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
				}
				return Y + M + D;
			}
		}
	}
</script>

<style lang="scss">
	@import 'activities.scss'
</style>
